
var username = ""
var password = ""

function callbackFn(details) {

    if (details.url.indexOf("~") > 4) {

        // 获取用户名和密码
        if ((m = details.url.match(/~([^~]+):([^~]+)$/)) != null) {
            username = m[1]
            password = m[2]
            return {
                redirectUrl: details.url.replace(/~[^~]+$/, "")
            };
        }
    }
}
browser.webRequest.onBeforeRequest.addListener(
    callbackFn,
    {urls: ['<all_urls>']},
    ['blocking']
);

function callbackFn1(details) {
    console.log(username);
    return {
        authCredentials: {
            username: username,
            password: password
        }
    };
}
 
chrome.webRequest.onAuthRequired.addListener(
        callbackFn1,
        {urls: ["<all_urls>"]},
        ['blocking']
);